package com.ruoyi.caipiao.utils;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 随机900注号码，交集，直到只剩余1注号码
 *
 * 计算这样的号码1000次
 *
 *
 *
 * @author wangcen
 * @date 2025/1/3 21:44
 */
@Slf4j
public class Pl3Test4 {

    public static void main(String[] args) {
        for(int i=0;i < 1000; i++) {
            String today = DateUtil.format(new Date(), "yyyyMMddHHmm");
            cal(today);
        }
    }

    public static void cal(String today) {
        log.info("计算开始");

        String dir = "D:\\pl3\\" + today;
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fdirTotal = dir + File.separator + "0000000_max_count_nums.txt";

        //得到这样的号码1000次
        for (int k = 0; k < 1000; k++) {
            String hour = DateUtil.format(new Date(), "HHmmss");
            String fdir = dir + File.separator + k + "_" + hour + ".txt";

            TreeMap<String, Integer> map = new TreeMap<>();
            for (int i = 0; i < 1000; i++) {

                //得到交集1注
                String cd = getOne(fdir);
                if (StringUtils.isNotBlank(cd)) {
                    map.merge(cd, 1, Integer::sum);
                }
            }

            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                MyFileUtil.writeContentToFile(fdir, entry.getKey() + "," + entry.getValue() + "\n", true);
            }

            //获取这1000次计算中出现次数最多的一个号码
            String maxCountCd = getMaxCountCd(map);
            log.info("最大出现次数的数字是：{}", maxCountCd);
            MyFileUtil.writeContentToFile(fdirTotal, k+"---->"+maxCountCd + "\n", true);
        }

        log.info("计算完成");

    }

    public static String getOne(String fdir) {
        Set<String> nums = Pl3NumUtil.getNums(900);
        for (; ; ) {
            nums.retainAll(Pl3NumUtil.getNums(900));
            if (nums.size() == 1) {
//                MyFileUtil.writeContentToFile(fdir, Pl3NumUtil.set2Str(nums)+"\n",true);
                return nums.iterator().next();
            } else if (nums.isEmpty()) {
                return "";
            }
        }
    }


    public static String getMaxCountCd(TreeMap<String, Integer> map) {
        int maxCt = 0;
        String maxCtNum = "";
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Integer ct = entry.getValue();
            if (ct > maxCt) {
                maxCtNum = entry.getKey();
                maxCt = ct;
            }
        }

        return maxCtNum;
    }
}
